<?php
// Pull in the NuSOAP code
require_once("vnpay/libs/nusoap.php");
require_once("config.php");
require_once("functions.php");

$client = new nusoap_client('http://112.78.7.141:1500/MVASCONTENT/MTSend.asmx?WSDL', true);
 $err = $client->getError();
      
      if ($err) {
         
         echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
         
         exit;
         
      }else {
         
         echo "<h2>Successfully connected</h2>";
         
      }
	  
$client->soap_defencoding = 'UTF-8';

/*
	
$flag="1";
$Total="1";   
$type="0";
$secu="123456";
$sendfrom ="8017";
$keyword="TD";
//================
$SeqNo="35707";
$dienThoai="84942875640";
$outcontent="Test sms AZ - azone.vn";

$param = array (
			"Destination" => $dienThoai, 
			"SendFrom" => $sendfrom,
			"KeywordName" => $keyword,
			"OutContent" => $outcontent, 
			"ChargingFlag" => $flag,
			"MOSeqNo" => $SeqNo,
			"TotalMessage" => $Total,
			"ContentType" => $type,
			"SecretCode" => $secu, 
		);
$result = $client->call('SendMT', $param);

echo '<p>'.print_r($result, true).'</p>';
echo $result['SendMTResult'] . $SeqNo;  
*/

$flag="1";
$Total="1";
$type="0";
$secu="123456";
$sendfrom ="8717";
$keyword="TD";
//===================
global $conf;	
$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
mysql_select_db($conf['db_name']);

$table = "dhcd_cky_mo";  //$table =    dhcd_vhh_mo 
$sqlSelect = "Select id, mo_id, telco, phone, message From `" . strtolower($table ) . "`";
$query=mysql_query($sqlSelect);

if(mysql_num_rows($query)  != 0)
{	
	while($row=mysql_fetch_array($query))	
	{	
		$SeqNo = $row['mo_id'];
		$telco = $row['telco'];
		$dienThoai = $row['phone'];
		$incom = $row['message'];
		//loc bo cac ky tu dac biet
		$incom = str_replace( ".", " ", $incom );
		$incom = str_replace( "-", " ", $incom );
		
		$outcontent= check_diemdh($SeqNo, $telco , $sendfrom, $keyword, $dienThoai, $incom);
		
		$param = array (
			"Destination" => $dienThoai, 
			"SendFrom" => $sendfrom,
			"KeywordName" => $keyword,
			"OutContent" => $outcontent,
			"ChargingFlag" => $flag,
			"MOSeqNo" => $SeqNo,
			"TotalMessage" => $Total,
			"ContentType" => $type,
			"SecretCode" => $secu,
		);
		$result = $client->call('SendMT', $param);
		
		echo '<p> STT: '.$row['id'].'</p>';
		echo '<p>'.print_r($result, true).'</p>';
		echo '<p>'.$outcontent.'</p>';
		echo $result['SendMTResult'] . $SeqNo;
	}
	//$sqlupdate = "UPDATE `" . strtolower($table ) . "` SET `status` = '1' ";
	//$qupdate=mysql_query($sqlupdate);
}

mysql_close($con);
//return $return;
echo '<p>Hoàn thành thao tác SMS chủ động!</p>';

/*$SeqNo="48112";
$dienThoai="84965616177";
$incom = "TD YTBB 01116";
$telco = "VIETTEL";
//$outcontent= check_diemdh($SeqNo, $telco , $sendfrom, $keyword, $dienThoai, $incom);
$outcontent= "Diem thi cua ban sbd=01116 : M1: 6.25, M2: 6.25, M3: 5.5. Soan tin DC YTB gui 8517 de xem diem chuan som nhat. Xem them http://kenh.xemtradiemthi.vn";
$param = array (
			"Destination" => $dienThoai, 
			"SendFrom" => $sendfrom,
			"KeywordName" => $keyword,
			"OutContent" => $outcontent,
			"ChargingFlag" => $flag,
			"MOSeqNo" => $SeqNo,
			"TotalMessage" => $Total,
			"ContentType" => $type,
			"SecretCode" => $secu,
		);
$result = $client->call('SendMT', $param);

echo '<p>'.print_r($result, true).'</p>';
echo '<p>'.$outcontent.'</p>';
echo $result['SendMTResult'] . $SeqNo;

===================*/


/*
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
*/

function check_diemdh($moid, $telco, $service_num, $syntax, $phone, $message)
{		
		global $conf;	
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		
		if(strpos($cuphap,"APP") !== false) {
			$cuphap = str_replace('APP','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"APA") !== false) {
			$cuphap = str_replace('APA','',$cuphap);
			$cuphap = trim($cuphap);
		}
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
		$b = 1;
		
		if(strpos($cuphap,"KT") !== false) {
			//Tra ket qua ve diem kiem tra lai cua thi sinh
			$cuphap = substr($cuphap, 2, strlen($cuphap) - 1);
			$cuphap = trim($cuphap);
			if (strpos($cuphap," ") == false){
			 	$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];		
			$b = 0;		
		}else {
			if (strpos($cuphap," ") == false){
				$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			//Tach Ma-Truong va So-Bao-Danh 
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];						
		}
		
		//kiem tra du lieu diem thi cua truong dh da co chua
		$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd_" . strtolower($matruongdh) . "'";
		$query=mysql_query($sqlcheck);
		$row=mysql_fetch_array($query);
		if ($row[0] == 0) {
			//chua co bang diem truong dh -> kiem tra bang luu cho cua truong dh
			$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd" . strtolower($matruongdh) . "_mo'";
			$qcheck=mysql_query($sqlcheck);
			$rcheck=mysql_fetch_array($qcheck);
			if ($row[0] == 0) {
				//Tao bang du lieu luu tru cho cua truong dh
				$sqlcreate = "CREATE TABLE IF NOT EXISTS `dhcd_" . strtolower($matruongdh) . "_mo` (
								  `id` bigint(20) NOT NULL AUTO_INCREMENT,
								  `mo_id` bigint(20) NOT NULL,
								  `telco` varchar(255) NOT NULL,
								  `service_num` varchar(255) NOT NULL,
								  `syntax` varchar(255) NOT NULL,  
								  `phone` varchar(255) NOT NULL,  
								  `message` varchar(255) NOT NULL,
								  `receive_date` datetime NOT NULL,
								  `sobaodanh` varchar(255) NOT NULL,
								  `status` tinyint(4) NOT NULL DEFAULT '0',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
				$qcreate=mysql_query($sqlcreate);
			}
			//Chen du lieu thi sinh vao bang luu tru cho cua truong dh
			$sqlInsert = "Insert Into `dhcd_" . strtolower($matruongdh) . "_mo`(mo_id, telco, service_num, syntax, phone, message, receive_date, sobaodanh, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), '" .cleanValue($sbd)."', 0)";
			mysql_query($sqlInsert);
			
			if ($b == 0){ $return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TD SBD gui 8717";}
			else{
				$return = "Tin nhan thanh cong. Du lieu cua truong " . $matruongdh . " dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://kenh.xemtradiemthi.vn";
			}
		}else{
			//Tra ket qua cho thi sinh
			$sqlSelect = "Select dhcd_dm1, dhcd_dm2, dhcd_dm3 From `dhcd_" . strtolower($matruongdh) . "` Where dhcd_sobaodanh = '" . trim($sbd) . "' And dhcd_khoi = '" . trim($khoi) . "'";
			$query=mysql_query($sqlSelect);
	
			if(mysql_num_rows($query)  != 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = "Diem thi cua ban sbd=". $sbd . " : M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Soan tin DC " . $matruongdh . " gui 8517 de xem diem chuan som nhat. Xem them http://kenh.xemtradiemthi.vn";
					if ($b == 0){$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Hay vao http://kenh.xemtradiemthi.vn/tuyensinh/box_tra_diem_thi/ de biet chi tiet";}
				}
			}else{
				$return = "Soan tin TD SBD gui 8717. Trong do SBD: Matruong+KhoiThi+So; doi voi khoi thi A1, D1.. SBD: Matruong+Khoithi So. VD: TD BKAA1234 gui 8717";
			}
		}
		
		mysql_close($con);
		return $return;
}

?>